const mongoose = require('mongoose');
const orderFormModel = mongoose.model("orderFormModel");
//获取所有订单
module.exports.getOrder = async function (params) {
    let rows=await orderFormModel
    .find()
    .populate('rentId')
    .populate('hireId')
    .populate('carId')
    .limit(params.pageNum - 0)
    .skip(params.pageNum * (params.currentPage - 1));
    let total = (await orderFormModel
    .find()
    .populate('rentId')
    .populate('hireId')
    .populate('carId')).length
    return {rows,total}
}

//根据查询条件获取订单
module.exports.getOrderSearch = async function ({currentPage,pageNum,inputVulue,selectType}) {
    if(selectType == "_id"){
        let rows =[]
        rows.push(await orderFormModel
        .findById(inputVulue)
        .populate('rentId')
        .populate('hireId')
        .populate('carId')
        .limit(pageNum - 0)
        .skip(pageNum * (currentPage - 1)));
        let total = (await orderFormModel
        .findById(inputVulue)
        .populate('rentId')
        .populate('hireId')
        .populate('carId')).length
        return {rows,total}
    }else{
        let rows=await orderFormModel
        .find({[selectType]:{$regex: inputVulue, $options: '$i' }})
        .populate('rentId')
        .populate('hireId')
        .populate('carId')
        .limit(pageNum - 0)
        .skip(pageNum * (currentPage - 1));
        let total = (await orderFormModel
        .find({[selectType]:{$regex: inputVulue, $options: '$i' }})
        .populate('rentId')
        .populate('hireId')
        .populate('carId')).length
        return {rows,total}
    }
    
}
//根据_id删除订单
module.exports.delOrder = async function(_id){
    return await orderFormModel.deleteOne(_id)
}

//根据_id查找修改订单
module.exports.checkOrder = async function(_id){
    return await orderFormModel.findOne(_id).populate('rentId')
    .populate('hireId')
    .populate('carId')
}